package com.ouwen.booking.service;

import com.ouwen.booking.po.Booking;
import com.ouwen.booking.repository.BookingRepository;
import org.apache.servicecomb.pack.omega.transaction.annotations.Compensable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.List;

/**
 * @author <a href="http://youngitman.tech">青年IT男</a>
 * @version v1.0.0
 * @className BookingServiceImpl
 * @description
 * @date 2019-12-16 15:50
 * @JunitTest: {@link  }
 **/
@Service
public class BookingServiceImpl implements IBookingService {

    @Autowired
    private BookingRepository bookingRepository;

    @Transactional
    @Compensable(compensationMethod = "cancel")
    @Override
    public boolean booking(Booking booking) {

        Assert.notNull(booking, "booking is not null");
        Assert.hasLength(booking.getPhone(), "phone is not null");
        //and so on ...

        bookingRepository.save(booking);

        return true;
    }

    @Transactional
    @Override
    public boolean cancel(Booking booking) {
        List<Booking> bookings = bookingRepository.findByUuid(booking.getUuid());
        if (bookings != null && bookings.size() > 0) {
            bookingRepository.delete(bookings.get(0));
        }
        return true;
    }
}
